function plot_rsma_results(user_bits, common_bits, private_bits, precoding, ...
    power_common, power_private, ber)
% plot_rsma_results - 绘制RSMA仿真结果
%
% 输入参数:
%   user_bits     - 用户原始比特的元胞数组
%   common_bits   - 公共部分比特的元胞数组
%   private_bits  - 私有部分比特的元胞数组
%   precoding     - 预编码结构体
%   power_common  - 公共流功率分配系数
%   power_private - 每个私有流功率分配系数
%   ber           - 用户误码率向量

% 获取用户数
num_users = length(user_bits);

% 创建图形窗口
figure('Name', 'RSMA仿真结果', 'Position', [100, 100, 1200, 800]);

% 绘制RSMA系统模型示意图
subplot(2, 3, 1);
% 简化的RSMA系统模型示意图
hold on;

% 绘制发射端
rectangle('Position', [0.1, 0.4, 0.2, 0.2], 'Curvature', [0.2, 0.2], 'FaceColor', [0.8, 0.8, 0.8]);
text(0.2, 0.5, '发射端', 'HorizontalAlignment', 'center');

% 绘制接收端
for u = 1:num_users
    y_pos = 0.7 - (u-1)*0.3;
    rectangle('Position', [0.7, y_pos, 0.2, 0.2], 'Curvature', [0.2, 0.2], 'FaceColor', [0.8, 0.8, 0.8]);
    text(0.8, y_pos+0.1, ['用户 ' num2str(u)], 'HorizontalAlignment', 'center');
end

% 绘制公共流
annotation('arrow', [0.3, 0.5], [0.5, 0.5], 'Color', 'r', 'LineWidth', 2);
annotation('textbox', [0.35, 0.52, 0.1, 0.05], 'String', '公共流', 'EdgeColor', 'none', 'Color', 'r');

% 绘制私有流
for u = 1:num_users
    y_end = 0.8 - (u-1)*0.3;
    annotation('arrow', [0.3, 0.5], [0.5, y_end], 'Color', 'b', 'LineWidth', 1.5);
    annotation('textbox', [0.4, y_end+0.02, 0.1, 0.05], 'String', ['私有流 ' num2str(u)], 'EdgeColor', 'none', 'Color', 'b');
end

% 绘制连接线
for u = 1:num_users
    y_pos = 0.8 - (u-1)*0.3;
    annotation('arrow', [0.5, 0.7], [y_pos, y_pos], 'Color', 'k', 'LineWidth', 1);
end

% 设置坐标轴
axis off;
title('RSMA系统模型示意图');
xlim([0, 1]);
ylim([0, 1]);

% 绘制RSMA消息分割示意图
subplot(2, 3, 2);
hold on;

bar_width = 0.3;
for u = 1:num_users
    % 计算各部分的大小
    total_bits = length(user_bits{u});
    common_size = length(common_bits{u}) / total_bits;
    private_size = length(private_bits{u}) / total_bits;
    
    % 绘制消息分割条形图
    x_pos = u;
    bar(x_pos-bar_width/2, [common_size, private_size], bar_width, 'stacked');
    
    % 添加标签
    text(x_pos-bar_width/2, common_size/2, '公共', 'HorizontalAlignment', 'center', 'Color', 'w');
    text(x_pos-bar_width/2, common_size+private_size/2, '私有', 'HorizontalAlignment', 'center', 'Color', 'w');
end

% 设置坐标轴
title('消息分割');
xlabel('用户');
ylabel('比例');
xlim([0.5, num_users+0.5]);
ylim([0, 1]);
xticks(1:num_users);
colormap(gca, [1 0 0; 0 0 1]);  % 红色和蓝色
grid on;

% 绘制功率分配柱状图
subplot(2, 3, 3);
hold on;

% 绘制功率分配
bar_data = [power_common, power_private*ones(1, num_users)];
bar(1:num_users+1, bar_data);

% 添加标签
xticks(1:num_users+1);
xticklabels(['公共流', arrayfun(@(x) ['用户' num2str(x)], 1:num_users, 'UniformOutput', false)]);
title('功率分配');
ylabel('功率比例');
ylim([0, 1]);
grid on;

% 绘制预编码向量可视化
subplot(2, 3, 4);
hold on;

% 获取预编码向量
w_c = precoding.common;
w_private = precoding.private;

% 计算预编码向量之间的内积矩阵
num_vectors = num_users + 1;
inner_product_matrix = zeros(num_vectors);

% 公共流与自身的内积
inner_product_matrix(1, 1) = abs(w_c' * w_c);

% 公共流与私有流的内积
for u = 1:num_users
    inner_product_matrix(1, u+1) = abs(w_c' * w_private{u});
    inner_product_matrix(u+1, 1) = inner_product_matrix(1, u+1);
end

% 私有流之间的内积
for i = 1:num_users
    for j = 1:num_users
        inner_product_matrix(i+1, j+1) = abs(w_private{i}' * w_private{j});
    end
end

% 显示内积矩阵
imagesc(inner_product_matrix);
colorbar;
colormap(gca, 'hot');

% 设置坐标轴
title('预编码向量内积矩阵');
xlabel('预编码向量');
ylabel('预编码向量');
xticks(1:num_vectors);
yticks(1:num_vectors);
xticklabels(['公共', arrayfun(@(x) ['私有' num2str(x)], 1:num_users, 'UniformOutput', false)]);
yticklabels(['公共', arrayfun(@(x) ['私有' num2str(x)], 1:num_users, 'UniformOutput', false)]);

% 绘制误码率柱状图
subplot(2, 3, 5);
hold on;

% 绘制各用户误码率
bar(1:num_users, ber);

% 设置坐标轴
title('用户误码率(BER)');
xlabel('用户');
ylabel('误码率');
xticks(1:num_users);
grid on;
set(gca, 'YScale', 'log');  % 使用对数尺度

% 绘制RSMA与其他技术的理论对比
subplot(2, 3, 6);
hold on;

% 生成SNR点
snr_dB_range = 0:2:30;
snr_linear = 10.^(snr_dB_range/10);

% 假设的信道参数
h1_squared = 1;     % 良好信道
h2_squared = 0.2;   % 弱信道

% 计算各方案的理论速率
% TDMA
rate_tdma_user1 = 0.5 * log2(1 + 2*h1_squared*snr_linear);
rate_tdma_user2 = 0.5 * log2(1 + 2*h2_squared*snr_linear);
rate_tdma_sum = rate_tdma_user1 + rate_tdma_user2;

% NOMA
rate_noma_user1 = log2(1 + h1_squared*snr_linear/(1 + 0.25*h1_squared*snr_linear));
rate_noma_user2 = log2(1 + 0.25*h2_squared*snr_linear);
rate_noma_sum = rate_noma_user1 + rate_noma_user2;

% RSMA (近似值)
rate_rsma_user1 = log2(1 + 0.7*h1_squared*snr_linear/(1 + 0.15*h1_squared*snr_linear));
rate_rsma_user2 = log2(1 + 0.7*h2_squared*snr_linear/(1 + 0.15*h2_squared*snr_linear));
rate_rsma_common = 0.3 * log2(1 + 0.15*min(h1_squared, h2_squared)*snr_linear);
rate_rsma_sum = rate_rsma_user1 + rate_rsma_user2 + rate_rsma_common;

% 绘制总和速率
plot(snr_dB_range, rate_tdma_sum, 'b-', 'LineWidth', 2, 'DisplayName', 'TDMA');
plot(snr_dB_range, rate_noma_sum, 'r-', 'LineWidth', 2, 'DisplayName', 'NOMA');
plot(snr_dB_range, rate_rsma_sum, 'g-', 'LineWidth', 2, 'DisplayName', 'RSMA');

% 设置坐标轴
title('多址接入技术理论速率比较');
xlabel('SNR (dB)');
ylabel('总和速率 (bps/Hz)');
grid on;
legend('Location', 'northwest');

% 添加总标题
sgtitle('RSMA (速率分割多址) 仿真结果');

end 